package org.kjtc.service;

import org.kjtc.entity.State;
import org.kjtc.entity.WorkState;
import org.kjtc.exception.SaveRuntimeException;
import org.kjtc.exception.UpdateRuntimeException;
import org.kjtc.mapper.SetUpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/13 0013.
 */
@Service
public class SetUpService {

    @Autowired
    private SetUpMapper setUpMapper;

    public List<State> getAllState(){
        List<State> lists=setUpMapper.getAllState();
        return lists;
    }
    public List<WorkState> getWorkState(){
        List<WorkState> lists=setUpMapper.getWorkState();
        return lists;
    }
    public int createState(State state){
        int num=setUpMapper.createState(state);
        if(num!=1){
            throw new SaveRuntimeException("添加失败");
        }
        return num;
    }
    public int deleteState(State state){
        int num=setUpMapper.deleteState(state);
        if(num!=1){
            throw new SaveRuntimeException("删除失败");
        }
        return num;
    }
    public int updateWorkState(WorkState workState){
        int num=setUpMapper.updateWorkState(workState);
        if(num!=1){
            throw new SaveRuntimeException("修改积极提醒时间失败");
        }
        return num;
    }
    public int updateState(String jsonStr) {
        int num = 0;
        Map<Integer, String> map = new HashMap<>();
        if (jsonStr != null && jsonStr != "") {
            String[] strs = jsonStr.split("\\;");
            for (String s : strs) {
                String[] ms = s.split(":");
                num += setUpMapper.updateState(Integer.valueOf(ms[0]),ms[1]);
                map.put(Integer.valueOf(ms[0]), ms[1]);
            }
        }
        if(num == 0){
            throw new UpdateRuntimeException("修改失败");
        }
        return num;
    }
}
